package org;

import org.BlockMap;
import org.newdawn.slick.Animation;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Color;
public class Player extends LevelObject {

	public static final float MOVE_SPEED = 0.1f;
	private static int size[] = {0, 0, 32, 0, 32, 32, 0, 32};  // dimension of player bounding box (poly)
	private float dx = 0;
	private float dy = 0;
	public Animation playerDefault,fallback;


	public Player(float x, float y, String type) throws SlickException {
		super(x, y,               
				(int)x + size[0], (int)y + size[1],
				(int)x + size[2], (int)y + size[3],
				(int)x + size[4], (int)y + size[5],
				(int)x + size[6], (int)y + size[7],
				type);
		// spritesheet and animation for the player
		SpriteSheet sheet = new SpriteSheet("data/karbonator.png", 32, 32);
		playerDefault = new Animation();
		for (int frame = 0; frame < 3; frame++) { playerDefault.addFrame(sheet.getSprite(frame, 0), 180); }
		playerDefault.setAutoUpdate(true);
		fallback = playerDefault;
	}

	
	public void update(int delta) throws SlickException {
		
		dx = (float) delta * MOVE_SPEED;
		dy = (float) delta * MOVE_SPEED;
	
		
		int col;
		if (Game.up){
			y -= dy;
			poly.setY(y);
			col=Game.entityCollisionWith(this);
			if (col>0){ // on arrete que si on touche le plafond
				y += dy;
				poly.setY(y);
			}
		}		
		if (Game.down){
			y += dy;
			poly.setY(y);
			col=Game.entityCollisionWith(this);
			if ((col&16)==16){ // on arrete que si on touche le sol
				y -= dy;
				poly.setY(y);
			}
		}
		if (Game.left){
			x -= dx;
			poly.setX(x + size[0]);
			col=Game.entityCollisionWith(this);
			if (col>0){ // on arrete que si on touche mur gauche ou sol par dessous(angle) ou plafond par dessus(angle)
				x += dx;
				poly.setX(x + size[0]);
			}
		}
		if (Game.right){
			x += dx;
			poly.setX(x + size[0]);

			col=Game.entityCollisionWith(this);
			if (col>0){ // on arrete que si on touche mur droite ou sol par dessous(angle) ou plafond par dessus(angle)
				x -= dx;
				poly.setX(x + size[0]);				
			}			
		}

		// ===============================
		// prevent player to cross borders
		// ===============================
		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		if (x > BlockMap.mapWidth - 32) {
			x = BlockMap.mapWidth - 32;
		}
		if (y > BlockMap.mapHeight - 32) {
			y = BlockMap.mapHeight - 32;
		}
	}
		
	

	
    public int isCollisionWith(LevelObject other) throws SlickException {
		if(poly.intersects(other.poly))
		{
			//poly.getCenter();
			//other.poly.getCenter();
			if(other.type=="floor")
			{
				if((poly.getCenterY()-other.poly.getCenterY())>=16)
					return 1; // dessus le sol
				else
					return 16; // dessous le sol
			}
			else if(other.type=="leftWall")
				return 2;
			else if(other.type=="rightWall")
				return 4;
			else if(other.type=="ceilling")
			{
				if((poly.getCenterY()-other.poly.getCenterY())>=16)
					return 8; // dessus le plafond
				else
					return 32; // dessous le plafond
			}
		}
		return 0; //pas de collision
    }

	public void draw(Graphics g) {
		g.setColor(Color.white);
		g.drawAnimation(fallback, x, y);
		g.draw(poly);
		g.drawString("onGround=["+onGround+"]", 50, 50);
	}
}


/*
main class: Game
depuis Game init() creation du map (BlockMap) 
ensuite update generale 
*/



